<!--字典管理页-->
<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">数据统计</span>
  </div>
  <!-- 字典数据搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="学校名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.schoolName" @keydown.enter="selectCount"></el-input>
      </el-form-item>
      <el-form-item label="学校id">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.id" @keydown.enter="selectCount"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="selectCount">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px;">
    <div style="margin:10px 0px 40px 10px;">
      统计列表
    </div>
    <el-table :data="countArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="schoolName" label="学校名称" align="center"/>
      <el-table-column prop="userName" label="用户" align="center">
      </el-table-column>
      <el-table-column prop="blogCount" label="帖子数量" align="center"/>
      <el-table-column prop="commentCount" label="评论数量" align="center"/>
    </el-table>
  </el-card>

  <!-- 保存字典弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典名称">
        <el-input placeholder="请输入" v-model="saveDictForm.name"></el-input>
      </el-form-item>
      <el-form-item label="字典编码">
        <el-input placeholder="请输入" v-model="saveDictForm.code"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入" v-model="saveDictForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose()">取消</el-button>
      <el-button type="primary" @click="saveDict()">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import router from "@/router";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const dialogVisible = ref(false);
const dialogTitle = ref('新增字典');

const countArr = ref([]);

onMounted(() => {
  selectCount();
})
//查询所有字典信息
const searchForm = ref({schoolName: '', id: '',});
const selectCount = () => {
  let data =qs.stringify(searchForm.value);
  console.log(data);
  console.log(searchForm.value)
  axios.get(BASE_URL+'/v1/school/selectCount?' + data).then((response) => {
    if (response.data.code == 2000) {
      countArr.value = response.data.data;
    } else {
      ElMessage(response.data.msg);
    }
  })
}
//重置用户搜索信息
const resetSearch = () => {
  searchForm.value = {};
  selectCount();
}

//保存字典信息
const saveDictForm = ref({name: '', code: '', remark: ''});
const saveDict = () => {
  dialogVisible.value = true;
  let data = qs.stringify(saveDictForm.value);
  axios.post(BASE_URL+'/v1/dict/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功!');
      dialogVisible.value = false;
      saveDictForm.value = {};
      selectDict();
    } else {
      ElMessage.error('保存失败!');
    }
  })
}
//新增字典
const addDict = ()=>{
  dialogVisible.value=true;
  dialogTitle.value = '新增字典';
}

//编辑字典信息
const editDict = (id) => {
  dialogTitle.value = '编辑字典';
  dialogVisible.value = true;
  axios.get(BASE_URL+'/v1/dict/select?id=' + id).then((response) => {
    if (response.data.code == 2000) {
      //注意！！！返回值是个list,记得加[0]
      saveDictForm.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//实现取消按钮的功能
const handleClose = () => {
  if (confirm('是否关闭本窗口?')) {
    //用户确认关闭后,将审批弹窗与确认驳回弹窗一起关掉
    dialogVisible.value = false;
    //取消时把填的数据都清空，表单回复到初始状态
    saveDictForm.value={};
  }
}

//删除字典
const deleteDict = (id) => {
  if (confirm('您确认要删除此字典吗?')) {
    axios.post(BASE_URL+'/v1/dict/delete/' + id).then((response) => {
      if (response.data.code === 2000) {
        ElMessage.success('删除成功!');
        selectDict();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

//点击不同的字典编码展示该字典下的具体字典项,携带当前字典的id跳转至字典项页即可
const showDictOption = (id) => {
  router.push('/dictOption?id=' + id);
}
</script>

<style scoped>

</style>